/**
 * Created with JetBrains PhpStorm.
 * User: Artem
 * Date: 12.03.13
 * Time: 17:56
 * To change this template use File | Settings | File Templates.
 */

Ext.define("Pro.controller.AddEmployee", {
    extend: "Ext.app.Controller",

    views: [
        "AddNewEmployee"
    ],

    stores: [
        "Teams"
    ],

    refs: [
        {
            ref: "backButton",
            selector: "addnewemployee #back"
        },
        {
            ref: "nextButton",
            selector: "addnewemployee #next"
        },
        {
            ref: "finishButton",
            selector: "addnewemployee #finish"
        },
        {
            ref: "addEmployeeFormPanel",
            selector: "addnewemployee #addEmployeeFormPanel"
        },
        {
            ref: "wizard",
            selector: "addnewemployee"
        },
        {
            ref: "respondTab",
            selector: "consolepanel #respond"
        },
        {
            ref: "requestTab",
            selector: "consolepanel #request"
        },
        {
            ref: "eventLogArea",
            selector: "consolepanel #eventLogArea"
        }
    ],

    init: function(){
        this.control({
            "addnewemployee #next": {
                click: this.onNextButtonClick
            },
            "addnewemployee #back": {
                click: this.onBackButtonClick
            },
            "addnewemployee #yesIWant": {
                change: this.onCheckBoxChange
            },
            "addnewemployee #teamBox": {
                change: this.onTeamBoxChange
            },
            "addnewemployee #finish": {
                click: this.onFinishButtonClick
            }
        });
    },

    onNextButtonClick: function(){
        var wizard = this.getWizard();
        var layout = wizard.getLayout();
        var backButton = this.getBackButton();
        var nextButton = this.getNextButton();
        var finishButton = this.getFinishButton();
        if (layout.getNext()){
            layout.next();
            backButton.enable();
            nextButton.disable();
            if (this.selectedEmployee){
                nextButton.hide();
                finishButton.show();
            }
        }
    },

    onBackButtonClick: function(){
        var wizard = this.getWizard();
        var layout = wizard.getLayout();
        var nextButton = this.getNextButton();
        var backButton = this.getBackButton();
        var finishButton = this.getFinishButton();
        if (layout.getPrev()){
            layout.prev();
            nextButton.enable();
            nextButton.show();
            finishButton.hide();
            if (!layout.getPrev()){
                backButton.disable();
            }
        }
    },

    onCheckBoxChange: function(obj, newValue, oldValue, eOpts){
        var nextButton = this.getNextButton();
        if (newValue){
            nextButton.enable();
        } else {
            nextButton.disable();
        }
    },

    onTeamBoxChange: function(obj, newValue, oldValue, eOpts){
        var nextButton = this.getNextButton();
        nextButton.enable();
        this.selectedEmployee = newValue;
    },

    onFinishButtonClick: function(){
        var addEmployeeFormPanel = this.getAddEmployeeFormPanel();
        var thisController = this;

        addEmployeeFormPanel.getForm().setValues({team: this.selectedEmployee});
        addEmployeeFormPanel.getForm().submit({
            url: "globallogic/create",
            success: function(form, action){
                Ext.MessageBox.alert(localization.MESSAGEBOX_SUCCESS,action.result.message);
                thisController.printRespond(action);
                thisController.printRequest(action);
            },
            failure: function(form, action){
                Ext.MessageBox.alert(localization.MESSAGEBOX_FAILURE,action.result.message);
                thisController.printRespond(action);
                thisController.printRequest(action);
            }
        });
        this.selectedEmployee = undefined;
        this.getWizard().close();
    },

    printRespond: function(action){
        var respondTab = this.getRespondTab();
        var eventLogArea = this.getEventLogArea();
        eventLogArea.setValue(eventLogArea.getValue() + (new Date()).toLocaleString() + " " + action.result.message + "\n");
        respondTab.setValue(action.response.responseText);
    },

    printRequest: function(action){
        var requestTab = this.getRequestTab();
        var req = "";
        req += ("url:\t\t\t" + action.response.request.options.url + "\n");
        req += ("method:\t\t" + action.response.request.options.method + "\n");
        req += "params: \n"
        req += ("\tteam:\t" + action.response.request.options.params.team + "\n");
        req += ("\tname:\t" + action.response.request.options.params.name + "\n");
        req += ("\tage:\t\t" + action.response.request.options.params.age + "\n");
        req += ("\tphone:\t" + action.response.request.options.params.phone + "\n");
        req += ("\tmarried:\t" + action.response.request.options.params.married + "\n");
        requestTab.setValue(req);
    }
});